

#include "app_photo.h"

//*******************************************************************************************

static image_para_t p_p;

//*******************************************************************************************

int32_t app_photo_edp_filenum(void)
{

	if(!p_p.p_fl){
		return 0;
	}

	if(!p_p.p_fl->f_total){
		return 0;
	}

	return p_p.p_fl->f_total;
}

int32_t app_photo_edp_switch(int32_t index)
{
	int ret  = -1;

	char photo_name[1024] = {0};
	
	//************************************************************************************
	memset(photo_name, 0, sizeof(photo_name));
	//************************************************************************************

	p_p.p_err = 0;
	//************************************************************************************

	ret  = filelist_get_filename(p_p.p_fl,photo_name,index);
	if((ret != 0)||(strlen(photo_name) <= 0))
	{	
		log_n("ret =%d\n",ret);

		p_p.p_err = 1;

		p_p.p_index ++ ;
		if(p_p.p_index >= app_photo_edp_filenum()){
			p_p.p_index = 0; 
		}

		return -1;
	}
	else
	{

	}
	//************************************************************************************
	log_d("i:t:n=%d:%d:%s\n",index,app_photo_edp_filenum(),photo_name);
	//************************************************************************************

	ret = decode_jpeg_and_show(photo_name);
	if(ret != 0){
		p_p.p_err = 1;
	}
	//************************************************************************************

	#if 0
	p_p.p_index ++ ;
	if(p_p.p_index >= app_photo_edp_filenum()){
		p_p.p_index = 0; 
	}
	#elif 1
	
	p_p.p_index = rand()%app_photo_edp_filenum();
	
	#endif

	photo_para_set_playindex(p_p.p_index);
	system_para_save();
	//************************************************************************************

	return 0;
	
}

void* app_photo_edp_task(void *arg)
{
	struct timeval timer;

	while(1)
	{
		usleep(1000);

		timer.tv_sec  = CFG_EDP_TIMEOUT;
		timer.tv_usec = 0;

		switch(select(0, NULL, NULL, NULL, &timer)) 
		{
		case -1:  
			log_e("Error!\n");
			break;
		case 0: 
			break;
		default:break;
		}
		//***********************************************

		app_photo_edp_switch(p_p.p_index);

		//***********************************************

	}

}


int32_t app_photo_edp_error(void)
{
	if(!p_p.p_err)
		return -1;

	if(p_p.p_err == 1)
	{
		app_photo_edp_switch(p_p.p_index);
	}


	p_p.p_err = 0;

	return 0;
}


void* app_photo_msg_task(void *arg)
{


	while(1)
	{
		usleep(1000);

		app_photo_edp_error();
	}
}





//*******************************************************************************************

int32_t app_photo_create(void)
{
	
	int  ret  = -1;
	//********************************************************************************
	log_n(" photo app create.\n");
	//********************************************************************************

	srand(time(NULL));
	
	//********************************************************************************

	p_p.p_index   = photo_para_get_playindex();
	p_p.p_err     = 0;

	log_n("edp_index =%d\n",p_p.p_index);
	//********************************************************************************

	p_p.p_fl = filelist_cjson_create(CFG_EDP_PATH,FILE_TYPE_PHOTO,"edp_file.tmp");
	if(!p_p.p_fl){
		log_e("create filelist error\n");
		return -1;
	}
	//********************************************************************************

	app_photo_edp_switch(p_p.p_index);
	//********************************************************************************

	ret=pthread_create(&p_p.p_thread, NULL, app_photo_edp_task, NULL);
	if(ret != 0){
		log_e("error=%s\n",strerror(ret));
	}
	
	//********************************************************************************
	ret=pthread_create(&p_p.p_msg_thread, NULL, app_photo_msg_task, NULL);
	if(ret != 0){
		log_e("error=%s\n",strerror(ret));
	}
	//********************************************************************************

	return 0;
}

int32_t app_photo_destory(void)
{

	if(p_p.p_fl){
		filelist_cjson_destory(p_p.p_fl);
		p_p.p_fl = NULL;
	}
	

	log_i("photo exit end\n");
		

	return 0;
}
